//
//  WXContactShopViewController.m
//  CarJX
//
//  Created by xcq on 14-10-20.
//  Copyright (c) 2014年 KK. All rights reserved.
//

#import "WXContactShopViewController.h"
#import "UIViewController+HttpRequest.h"
#import "WXRoadHelpTableViewCell.h"
#define kTableFrame CGRectMake(0, self.navigationController.navigationBar.frame.size.height + 13, self.view.frame.size.width, self.view.frame.size.height - self.navigationController.navigationBar.frame.size.height - 60)
#define kDefaultShopPic [UIImage imageNamed:@"ios2_r1_c2_s1"]



@interface WXContactShopViewController ()<UIAlertViewDelegate>

@end

@implementation WXContactShopViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidDisappear:(BOOL)animated{
     [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"seleted"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void) seletedPushMethod : (NSNotification *) notification{
 
    NSString *seleted = notification.object;

    NSLog(@"%@", [_tableDataSource objectAtIndex:[seleted integerValue]] );
    _seleteId =[[_tableDataSource objectAtIndex:[seleted integerValue]] objectForKey:@"ID"];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(seletedPushMethod:) name:@"seletedpuss" object:nil];
     
    [self setTitle:@"预约商家"];
    
    [self GETWithContentHelpType:@"wzdb" params:@{} success:^(ResultMy *result) {
        
        [self.view hideToastActivity];

        _tableDataSource = [NSMutableArray arrayWithArray:[result.dictionaryInfo objectForKey:@"seller"]];
        [self initWithTableView];
        
    } failure:^(NSError *error) {
        [self.view hideToastActivity];
    }];
    [self.view makeToastActivity];
    
 }

- (void) initWithTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:kTableFrame style:UITableViewStylePlain];
    NSLog(@"%@",NSStringFromCGRect(kTableFrame));
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"WXRoadHelpTableViewCell" bundle:nil] forCellReuseIdentifier:@"roadCell"];
    [self.view addSubview:_tableView];
    
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
 }

- (IBAction)requestShopAction:(id)sender {
    if (_seleteId == nil) {
        [self.view makeToast:@"您还没有选择商铺哦!" duration:1.5f position:TOP_POINT];
        return;
    }
    if ([self.identity isEqualToString:@"网上获取"]) {
        [self.view makeToastActivity];
        [self GETWithContentType:@"imibreakrual" params:@{@"member_id" : [Common currentUserId] , @"seller_id" : _seleteId ,@"imgurl":_imageURl} success:^(ResultMy *result) {
            [self.view makeToast:@"预约成功" duration:1.8f position:TOP_POINT];
            [self.view hideToastActivity];
        } failure:^(NSError *error) {
            [self.view makeToast:@"预约失败" duration:1.8f position:TOP_POINT];
            [self.view hideToastActivity];
        }];
    }else{
        
        if (_seletedTraffic) {
            NSString *joint = @"";
            NSString *price = @"";
            CGFloat price1 = 0;
            NSString *str = @",";
            int i = 0;
            for (NSDictionary *dic in [_seletedTraffic allValues]) {
                price1 += [[dic objectForKey:@"price"] integerValue];
                if (i == ([[_seletedTraffic allValues] count] - 1)) {
                    joint = [joint stringByAppendingString:[dic objectForKey:@"id"]  ];
                    
                }else{
                    joint = [joint stringByAppendingString:[[dic objectForKey:@"id"] stringByAppendingString:str]];
                }
                i ++;
            }
            
            price = [NSString stringWithFormat:@"%.3f",price1];
            [self GETWithContentHelpType:@"wzdeal" params:@{@"memberid" : [Common currentUserId] , @"content" : joint, @"base_price" : price , @"shopid" : _seleteId} success:^(ResultMy *result) {
                [self.view makeToast:@"预约成功" duration:1.8f position:TOP_POINT];
                [self.view hideToastActivity];
                
            } failure:^(NSError *error) {
                [self.view makeToast:@"预约失败" duration:1.8f position:TOP_POINT];
                [self.view hideToastActivity];
                
            }];
            [self.view makeToastActivity];
        }
    }
    
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
    }else{
        CLUIBaseNavigationController *nav = [[CLUIBaseNavigationController alloc]  initWithRootViewController:[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"login"]];
        [self presentViewController:nav animated:YES completion:^{
            
        }];
        
    }
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 115;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (!_cellSelete) {
        _cellSelete = [[NSMutableDictionary alloc] initWithCapacity:_tableDataSource.count];
    }else{
        [_cellSelete removeAllObjects];
        
    }
    
    return [_tableDataSource count] >0 ? [_tableDataSource count] : 1;
}


/*
 
 /*
 "LAT": "关注",
 "SELLER_NUMBER": "下单",
 "LON": "距离",
 "ID": "1",
 "LEVEL": "等级",
 "SHOP_NAME": ’店铺名’,
 "BUSITYPE": "营业类型",
 "SHOP_LOGO": "http://sys.clejw.com/upload/image/201409/ec393984-6eed-473b-91b5-322d55e75b95.png"
 
 {
 BUSITYPE = "\U8fdd\U7ae0\U4ee3\U529e \U670d\U52a1";
 ID = 199;
 LAT = 0;
 LEVEL = 0;
 LON = "114.369517";
 "SELLER_NUMBER" = 0;
 "SHOP_ADDR" = "\U4e2d\U56fd-\U5149\U8c37\Uff0c\U78a7\U6ce2\U6c34\U65cf\U9986";
 "SHOP_LOGO" = "http://sys.clejw.com/uploadhttp://seller.clejw.com/upload/image/201410/30965d53-5b88-4ebf-84e2-2d4ed94515c0.png";
 "SHOP_NAME" = "\U6d4b\U8bd5\U5e97\U94fa3";
 }
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    WXRoadHelpTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"roadCell" forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.enshrineBtn.hidden = NO;
    cell.enshrineBtn.tag = 600 + indexPath.row;
    cell.tag = 600 + indexPath.row;
    
    NSLog(@"%d _  %@", indexPath.row , cell.enshrineBtn);
    [_cellSelete setObject:cell.enshrineBtn forKey:[NSString stringWithFormat:@"%d",cell.enshrineBtn.tag]];

    
    NSDictionary *dic = [_tableDataSource objectAtIndex:indexPath.row];
    
    cell.companyName.text = [dic objectForKey:@"SHOP_NAME"];
    
    if ([dic objectForKey:@"SHOP_LOGO"] == [NSNull null]) {
        
        cell.companyImage.image = kDefaultShopPic;
        
    }else{
        
        [cell.companyImage setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"SHOP_LOGO"]] placeholderImage:kDefaultShopPic];
        
    }
    
    switch ([[dic objectForKey:@"LEVEL"] integerValue]) {
        case 1:
            cell.evaluationStart_two.hidden = YES;
            cell.evaluationStart_three.hidden = YES;
            cell.evaluationStart_four.hidden = YES;
            cell.evaluationStart_five.hidden = YES;
            break;
            
        case 2:
            
            cell.evaluationStart_three.hidden = YES;
            cell.evaluationStart_four.hidden = YES;
            cell.evaluationStart_five.hidden = YES;
            
            break;
            
        case 3:
            
            cell.evaluationStart_four.hidden = YES;
            cell.evaluationStart_five.hidden = YES;
            break;
        case 4:
            
            cell.evaluationStart_five.hidden = YES;
            break;
        case 5:
            
            break;
            
    }
    NSArray *busitype = [[dic objectForKey:@"BUSITYPE"] componentsSeparatedByString:@" "];
    switch ([busitype count]) {
        case 1:
            cell.serviceOne.text = busitype[0];
            break;
        case 2:
            cell.serviceOne.text = busitype[0];
            cell.serviceTwo.text = busitype[1];
            
            
            break;
        case 3:
            cell.serviceOne.text = busitype[0];
            cell.serviceTwo.text = busitype[1];
            cell.serviceThre.text = busitype[2];
            
            break;
        case 4:
            
            cell.serviceOne.text = busitype[0];
            cell.serviceTwo.text = busitype[1];
            cell.serviceThre.text = busitype[2];
            cell.serviceFour.text = busitype[3];
            
            break;

            
            
        default:
            
            break;
    }
    cell.focusNum.text = [dic objectForKey:@"LAT"];
    cell.placeOrderNum.text = [dic objectForKey:@"SELLER_NUMBER"];
    cell.DistanceNum.text =  [[NSString stringWithFormat:@"%.2f",[[dic objectForKey:@"LON"] floatValue] / 1000 ] stringByAppendingString:@"Km"];
    if (indexPath.row == [_tableDataSource count] - 1) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"cellSeleted" object:_cellSelete];
        
//        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_cellSelete];
//        [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"cellSeleted"];
//        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    return  cell;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
    
}

@end
